package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageResult;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import org.omg.CORBA.Object;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@CrossOrigin
@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    //查询所有
    @GetMapping
    public List<Employee> findAll(){

        return employeeService.findAll();
    }

    //分页
    @PostMapping
    public PageResult<Employee> employeePageResult( @RequestBody EmployeeQuery employee){
        //System.out.println(employee);
        //System.out.println(employeeService.queryPage(employee));
        return employeeService.queryPage(employee);
    }
    //删除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        AjaxResult ajaxResult = new AjaxResult();

        try {
            employeeService.remove(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxResult.setMessage(e.getMessage());
        }
        return ajaxResult;
    }

    //批量删除
    @PatchMapping
    public AjaxResult removes(@RequestBody List<Long> ids){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            employeeService.removes(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxResult.setMessage(e.getMessage());
        }
        return ajaxResult;
    }

    //添加和修改
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId()!=null){
                employeeService.update(employee);
            }else {
                employeeService.save(employee);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage(e.getMessage());
        }
        return new AjaxResult();
    }

}
